/**   
 * @Title: AccountDetailListAdapter.java 
 * @Package: com.esun.rabbit2.viewer.mainact.mylotteryact.accountdetail 
 * @Description: 账户明细列表adapter类
 * @Author: Billy qigs@500wan.com   
 * @Date: 2011-8-2 下午12:14:23  
 * Copyright (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.rabbit2.viewer.mainact.mylotteryact.accountdetail;

import java.util.ArrayList;
import java.util.Hashtable;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.esun.config.Config;
import com.esun.config.Key;
import com.esun.config.Language;
import com.esun.rabbit2.viewer.mainact.mylotteryact.lotteryrecord.LotteryKindJudgment;
import com.esun.ui.R;
import com.esun.util.log.EDebug;

/**
 * @ClassName: AccountDetailListAdapter
 * @Description: 账户明细列表adapter类
 * @Author: Billy qigs@500wan.com
 * @Date: 2011-8-2 下午12:14:23
 */
public class AccountDetailListAdapter extends BaseAdapter {

	private Context context;
	private LayoutInflater listInflater;
	private ArrayList<Hashtable<String, String>> list = new ArrayList<Hashtable<String, String>>();

	private TextView tvDate;
	private TextView tvTime;
	private TextView tvType;
	private TextView tvLotID;
	private TextView tvMoney;
	private TextView tvBalance;

	private LinearLayout linearLotID;
	private LinearLayout linearTitle;
	private RelativeLayout linearInfo;
	private ImageView ivArrow;

	private int groupCount = 0;
	private int childCount = 0;

	public AccountDetailListAdapter(Context con, ArrayList<Hashtable<String, String>> tempList) {
		context = con;
		this.listInflater = LayoutInflater.from(context);
		this.list = tempList;
	}

	@Override
	public int getCount() {
		
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		
		return null;
	}

	@Override
	public long getItemId(int position) {
		
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		

		convertView = (LinearLayout) listInflater.inflate(R.layout.accountdetail_list_table, null);

		linearInfo = (RelativeLayout) convertView.findViewById(R.id.accountdetail_listtable_relative_info);
		linearTitle = (LinearLayout) convertView.findViewById(R.id.accountdetail_listtable_linear_title);
		linearLotID = (LinearLayout) convertView.findViewById(R.id.accountdetail_listtable_linear_lotid);
		ivArrow = (ImageView) convertView.findViewById(R.id.accountdetail_listtable_iv_arrow);
		tvTime = (TextView) convertView.findViewById(R.id.accountdetail_listtable_tv_time);
		tvDate = (TextView) convertView.findViewById(R.id.accountdetail_listtable_tv_title);
		tvType = (TextView) convertView.findViewById(R.id.accountdetail_listtable_tv_type);
		tvLotID = (TextView) convertView.findViewById(R.id.accountdetail_listtable_tv_lotid);
		tvBalance = (TextView) convertView.findViewById(R.id.accountdetail_listtable_tv_balance);
		tvMoney = (TextView) convertView.findViewById(R.id.accountdetail_listtable_tv_money);

		/*
		 * 判断是不是同一天 加载不同的背景 还需要考虑下
		 */
		if (position == 0) {
			linearTitle.setVisibility(View.VISIBLE);
			EDebug.o(this, "position=" + position);
			EDebug.o(this, "这是第一条");
		} else if (!list.get(position).get("date").equals(list.get(position - 1).get("date"))) {
			linearTitle.setVisibility(View.VISIBLE);
			EDebug.o(this, "position=" + position);
			EDebug.o(this, "与前一条不是同一天");
			EDebug.o(this, "position=" + position + "date=" + list.get(position).get("date"));
			EDebug.o(this, "position-1=" + (position - 1) + "date=" + list.get(position - 1).get("date"));
		} else if (list.get(position).get("date").equals(list.get(position - 1).get("date"))) {
			EDebug.o(this, "position=" + position);
			EDebug.o(this, "与上一条记录是同一天");
			EDebug.o(this, "position=" + position + "date=" + list.get(position).get("date"));
			EDebug.o(this, "position-1=" + (position - 1) + "date=" + list.get(position - 1).get("date"));
		} else {
			EDebug.o(this, "position=" + position);
			EDebug.o(this, "其他情况");
			EDebug.o(this, "position=" + position + "date=" + list.get(position).get("date"));
			EDebug.o(this, "position-1=" + (position - 1) + "date=" + list.get(position - 1).get("date"));
		}

		// 判断交易类型
		typeIDJusty(list.get(position).get("typeid"), list.get(position).get("lotid"), linearLotID, ivArrow);

		// 判断收入还是支出
		tvMoney.setText("￥" + list.get(position).get("money"));
		if (list.get(position).get("inout").equals("1")) {
			Drawable left = context.getResources().getDrawable(R.drawable.icon_default_accountdetails_cost_31x27);
			left.setBounds(0, 0, left.getIntrinsicWidth(), left.getIntrinsicHeight());
			tvMoney.setCompoundDrawables(left, null, null, null);
			tvMoney.setTextColor(Color.RED);
		} else if (list.get(position).get("inout").equals("0")) {
			Drawable left = context.getResources().getDrawable(R.drawable.icon_default_accountdetails_income_31x27);
			left.setBounds(0, 0, left.getIntrinsicWidth(), left.getIntrinsicHeight());
			tvMoney.setCompoundDrawables(left, null, null, null);
			tvMoney.setTextColor(Color.GREEN);
		}

		tvDate.setText(list.get(position).get("date"));
		tvTime.setText(list.get(position).get("time"));
		tvBalance.setText("￥" + list.get(position).get("balance"));
		tvType.setText(list.get(position).get("note"));
		tvLotID.setText(lotNameJustic(list.get(position).get("lotid")));

		return convertView;
	}

	public void countCalculate() {
		groupCount = 0;
		childCount = 0;
		for (int i = 0; i < list.size(); i++) {
			if (i == 0) {
				groupCount++;
			} else if (!list.get(i).get(Key.KEY_DATE).equals(list.get(i - 1).get(Key.KEY_DATE))) {
				groupCount++;
			} else {
				childCount++;
			}
		}
	}

	public int getGroupCount() {
		return groupCount;
	}

	public int getChildCount() {
		return childCount;
	}

	// 判断是那种操作行为
	public void typeIDJusty(String typeid, String lotid, LinearLayout lotname, ImageView arrow) {

		linearInfo.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.cyhm_listbg_img_6x89));
		LotteryKindJudgment judgment = new LotteryKindJudgment();
		int tag = 1;// 彩种判断方法参数，账户明细处传1、购彩记录处传2
		switch (Integer.valueOf(typeid)) {
		case 1:// 购彩

			lotname.setVisibility(View.VISIBLE);
			// 判断是否是可支持彩种，设置不同点击效果

			if (judgment.lotterykindsjudgment(Integer.parseInt(lotid), tag)) {
				arrow.setVisibility(View.VISIBLE);
				linearInfo.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.relative_gcjl_bg_style));
			}

			break;

		case 52:// 奖金派送

			lotname.setVisibility(View.VISIBLE);
			// 判断是否是可支持彩种，设置不同点击效果
			if (judgment.lotterykindsjudgment(Integer.parseInt(lotid), tag)) {
				arrow.setVisibility(View.VISIBLE);
				linearInfo.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.relative_gcjl_bg_style));
			}

			break;

		case 46:// 撤单

			lotname.setVisibility(View.VISIBLE);
			// 判断是否是可支持彩种，设置不同点击效果
			if (judgment.lotterykindsjudgment(Integer.parseInt(lotid), tag)) {
				arrow.setVisibility(View.VISIBLE);
				linearInfo.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.relative_gcjl_bg_style));
			}

			break;

		case 20:// 其他添加

			break;

		case 30:// 用户提款

			break;

		case 42:// 解除提款冻结

			break;

		case 38:// 提款冻结

			break;

		default:

			break;
		}
	}

	public String lotNameJustic(String lotid) {
		String strLotName = "";
		EDebug.o(this, "lottery id = " + lotid);
		if (lotid.equals(Config.SHUANGSEQIU)) {// 双色球
			strLotName = Language.LOTNAME_SHUANGSEQIU;
		} else if (lotid.equals(Config.DALETOU)) {// 大乐透
			strLotName = Language.LOTNAME_DALETOU;
		} else if (lotid.equals(Config.SHIYIXUANWU)) {// 11选5
			strLotName = Language.LOTNAME_SHIYIXUANWU;
		} else if (lotid.equals(Config.JINGCAIZUQIU)) {// 精彩足球
			strLotName = Language.LOTNAME_JINGCAIZUQIU;
		} else if (lotid.equals(Config.ERSHIERXUANWU)) {// 22选5
			strLotName = Language.LOTNAME_22XUAN5;
		} else if (lotid.equals(Config.SHENGFUCAI)) {// 胜负彩
			strLotName = Language.LOTNAME_ZUQIUSHENGFU;
		} else if (lotid.equals(Config.BEIJINGZUQIUDANCHANG)) {// 北单
			strLotName = Language.LOTNAME_ZUQIUDANCHANG;
		} else if (lotid.equals(Config.RENXUANJIU)) {// 任九
			strLotName = Language.LOTNAME_RENXUAN9CHANG;
		} else if (lotid.equals(Config.LIUCHANGBANQUAN)) {// 6场半全
			strLotName = Language.LOTNAME_6CHANGBANQUAN;
		} else if (lotid.equals(Config.SICHANGJINQIU)) {// 4场进球
			strLotName = Language.LOTNAME_4CHANGJINQIU;
		} else if (lotid.equals(Config.FUCAI3D)) {// 福彩3d
			strLotName = Language.LOTNAME_FUCAI3D;
		} else if (lotid.equals(Config.PAILIESAN)) {// 数字排列
			strLotName = Language.LOTNAME_PAILIE;
		} else if (lotid.equals(Config.PAILIEWU)) {// 排列5
			strLotName = Language.LOTNAME_PAILIE5;
		} else if (lotid.equals(Config.QILECAI)) {// 七乐彩
			strLotName = Language.LOTNAME_QILECAI;
		} else if (lotid.equals(Config.QIXINGCAI)) {// 七星彩
			strLotName = Language.LOTNAME_QIXINGCAI;
		} else if (lotid.equals(Config.SHISHICAI)) {// 时时彩
			strLotName = Language.LOTNAME_SHISHICAI;
		} else if (lotid.equals(Config.JINGCAILANQIU)) {// 竞彩篮球
			strLotName = Language.LOTNAME_JINGCAILANQIU;
		} else if (lotid.equals(Config.KUAILESHIFEN)) {// 竞彩篮球
			strLotName = Language.LOTNAME_KUAILESHIFEN;
		} else {
			EDebug.o(this, "彩种id出错  lottery id = " + lotid);
		}

		return strLotName;
	}
}
